package com.lcu.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcu.reggie.common.BaseContext;
import com.lcu.reggie.common.R;
import com.lcu.reggie.dto.OrdersDto;
import com.lcu.reggie.entity.OrderDetail;
import com.lcu.reggie.entity.Orders;
import com.lcu.reggie.entity.User;
import com.lcu.reggie.service.OrderDetailService;
import com.lcu.reggie.service.OrderService;
import com.lcu.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private UserService userService;


    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){

        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return R.success("结算成功");
    }

    /**
     * 在主页返回订单信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(int page,int pageSize){
        log.info("page:{}",page);
        log.info("pageSize:{}",pageSize);

        Page<Orders> pageInfo=new Page<>(page,pageSize);
        Page<OrdersDto> ordersDtoPage=new Page<>();

        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,ordersDtoPage,"recorde");

        //找出当前Orders的数据
        List<Orders> records = pageInfo.getRecords();
        //对Orders的数据进行遍历
        List<OrdersDto> list=records.stream().map((item)->{
            OrdersDto ordersDto=new OrdersDto();

            //将item的数据拷贝到orderDto
            BeanUtils.copyProperties(item,ordersDto);

            //将orderDto的数据填满
            Long userId = item.getUserId();
            User user = userService.getById(userId);
            String userName = user.getName();
            String phone = user.getPhone();
            ordersDto.setUserName(userName);
            ordersDto.setPhone(phone);

            String address = item.getAddress();
            ordersDto.setAddress(address);
            String consignee = item.getConsignee();
            ordersDto.setConsignee(consignee);

            Long itemId = item.getId();
            LambdaQueryWrapper<OrderDetail> queryWrapper1=new LambdaQueryWrapper<>();
            queryWrapper1.eq(OrderDetail::getOrderId,itemId);
            List<OrderDetail> list1 = orderDetailService.list(queryWrapper1);
            ordersDto.setOrderDetails(list1);

            return ordersDto;

        }).collect(Collectors.toList());


        ordersDtoPage.setRecords(list);
        return R.success(ordersDtoPage);
    }

}
